#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Automatically run unit tests when the code is changed"""
__author__ = "Lei Chen <hide1713@gmail.com>"
import glob,os,stat,time, logging

'''
Watch for changes in all .py files. If changes, run unittest. 
'''
def checkSum():
    ''' Return a long which can be used to know if any .py files have changed.
    Only looks in the current directory. '''
    val = 0
    for f in glob.glob ('../src/*.py'):
        stats = os.stat (f)
        val += stats [stat.ST_SIZE] + stats [stat.ST_MTIME]

    for f in glob.glob ('*.py'):
        stats = os.stat (f)
        val += stats [stat.ST_SIZE] + stats [stat.ST_MTIME]
    return val

val=0
while (True):
    if checkSum() != val:
        val=checkSum()
        r = os.system ('python ../test/unit_test_suite.py')
        #play a warning sound if test fail
        if r != 0:
            os.system("aplay /usr/share/sounds/speech-dispatcher/test.wav")
    time.sleep(1)
